/**   
 * 文件名：PostAction.java</br>
 * 描述： </br>
 * 开发人员：汪浩博 </br>
 * 创建时间： 2016-5-5
 */ 

package com.post.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import net.sf.json.JSONObject;

import com.base.BaseAction.BaseAction;
import com.base.util.DateUtil;
import com.base.util.Page;
import com.base.util.TopicEnum;
import com.opensymphony.xwork2.ModelDriven;
import com.post.bean.Carousel;
import com.post.bean.Post;
import com.post.bean.PostVO;
import com.post.service.PostService;
import com.user.service.UserService;

/** 
 * 类名: PostAction</br> 
 * 包名：com.post.action </br> 
 * 描述: 帖子类Action</br>
 * 发布版本号：</br>
 * 开发人员： 汪浩博</br>
 * 创建时间： 2016-5-5 
 */

public class PostAction extends BaseAction implements ModelDriven<Post>{
	
	private Post post = new Post();
	private Integer pagenum = 1;
	private String topicId;		//主题id
	private PostService postService;
	private UserService userService;
	
	private String pid;			//帖子id
	private String nickName;	//用户昵称
	private String topicName;	//主题名
	
	/**
	 * 方法名: getPageResult</br>
	 * 详述: 前台分页查询帖子</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-5</br>
	 * @return
	 */
	public String getPageResult(){
		PostVO postVo = new PostVO();
		String topicId2 = this.getRequest().getParameter("topicId");//获取url参数，主题id
		//按时间筛选
		String date = this.getRequest().getParameter("date");//获取url参数
		//排序
		String sort = this.getRequest().getParameter("sort");//获取url参数
		String session_topicId = (String) this.getSession().get("topicId");
		if(null == session_topicId || !session_topicId.equals(topicId2)){
			this.getSession().put("topicId", topicId2);//把topicId存到session中
			String topicName = TopicEnum.judgeTopic(topicId2);
			this.getSession().put("topicName", topicName);
			postVo.setTopicId(topicId2);
		}else{
			postVo.setTopicId(session_topicId);
		}
		Page querypage = postService.Querypage(postVo, pagenum, pagesize,date,sort,false);
		this.getRequest().setAttribute("page", querypage);
		return "list";
	}
	/**
	 * 方法名: getPageResult2</br>
	 * 详述: 后台管理分页查询</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-3</br>
	 * @return
	 */
	public String getPageResult2(){
		String title = this.getRequest().getParameter("title");
		String status = this.getRequest().getParameter("postStatus");
		PostVO postVo = new PostVO();
		postVo.setTitle(title);
		postVo.setPostStatus(status);
		postVo.setNickName(nickName);
		postVo.setTopicName(topicName);
		Page querypage = postService.Querypage(postVo, pagenum, pagesize,null,null,true);
		this.getRequest().setAttribute("page", querypage);
		return "backlist";
	}
	/**
	 * 方法名: getPageResult3</br>
	 * 详述: 个人中心里分页查询帖子</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-11</br>
	 * @return
	 */
	public String getPageResult3(){
		JSONObject json = new JSONObject();
		String uid = this.getRequest().getParameter("uid");
		Page querypage = postService.findPagePostByUid(Long.parseLong(uid), pagenum, pagesize);
		json.put("page", querypage);
		this.writeJSON(json.toString());
		return null;
	}
	
	public String getPageResult4(){
		String title = this.getRequest().getParameter("title");
		String status = this.getRequest().getParameter("postStatus");
		PostVO postVo = new PostVO();
		postVo.setTitle(title);
		postVo.setPostStatus(status);
		Page querypage = postService.Querypage(postVo, pagenum, pagesize,null,null,true);
		this.getRequest().setAttribute("page", querypage);
		return "carouseladd";
	}
	/**
	 * 方法名: getSearchPost</br>
	 * 详述: 查询帖子</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-13</br>
	 * @return
	 */
	public String getSearchPost(){
		String title = this.getRequest().getParameter("title");
		try {
			//解决乱码
			title = new String(title.getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		PostVO postVo = new PostVO();
		postVo.setTitle(title);
		Page querypage = postService.Querypage(postVo, pagenum, pagesize,null,null,false);
		this.getRequest().setAttribute("page", querypage);
		this.getRequest().setAttribute("keyword", title);
		return "searchPost";
	}
	/**
	 * 方法名: release</br>
	 * 详述: 发布帖子</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-19</br>
	 * @return
	 */
	public String release(){
		post.setReplyNumber(0);
		post.setViewNumber(0);//设置默认参数
		post.setPostStatus("0");
		post.setPostTime(DateUtil.getStringTime());
		post.setLastreplytime(DateUtil.getStringTime());
		String userName = this.getLoginUser().getUserName();
		pid = userName + DateUtil.getStringTime3();
		post.setId(pid);//用户名+时间来作为id
		postService.addPost(post);
		return "postrelease";
	}
	/**
	 * 方法名: personPost</br>
	 * 详述: 查询个人相关帖子</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-20</br>
	 * @return
	 */
	public String personPost(){
		Long id = this.getLoginUser().getId();
		Page querypage = postService.findPagePostByUid(id,pagenum,pagesize);
		this.getRequest().setAttribute("page", querypage);
		return "personPost";
	}
	/**
	 * 方法名: deletePost</br>
	 * 详述: 通过id删除帖子</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-5</br>
	 */
	public String deletePost(){
		postService.deletePost(pid);
		return "deletepost";
	}
	/**
	 * 方法名: updatePostStatus</br>
	 * 详述: 更新帖子状态</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-6</br>
	 * @return
	 */
	public String updatePostStatus(){
		String status = this.getRequest().getParameter("status");
		postService.updatePostStatus(status, pid);
		return "updatePostStatus";
	}
	/**
	 * 方法名: getAllCarousel</br>
	 * 详述: 获取所有轮播对象</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-13</br>
	 * @return
	 */
	public String getAllCarousel(){
		List<Carousel> allCarousel = postService.getAllCarousel();
		this.getRequest().setAttribute("allCarousel", allCarousel);
		return "allcarousel";
	}
	
	public String addCarousel(){
		this.getRequest().getParameter("");
		this.getRequest().getParameter("");
		this.getRequest().getParameter("");
		this.getRequest().getParameter("");
		return "addcarousel";
	}
	
	public String deleteCarousel(){
		String[] carouselids = this.getRequest().getParameterValues("carouselid");
		for (String carouselid : carouselids) {
			Carousel carousel = postService.getSingleCarousel(carouselid);
			postService.deleteCarousel(carousel);
		}
		return "deleteCarousel";
	}
	
	@Override
	public Post getModel() {
		return this.post;
	}

	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public PostService getPostService() {
		return postService;
	}

	public void setPostService(PostService postService) {
		this.postService = postService;
	}

	public Integer getPagenum() {
		return pagenum;
	}

	public void setPagenum(Integer pagenum) {
		this.pagenum = pagenum;
	}

	public String getTopicId() {
		return topicId;
	}
	public void setTopicId(String topicId) {
		this.topicId = topicId;
	}
	public String getPid() {
		return pid;
	}
	public void setPid(String pid) {
		this.pid = pid;
	}
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getTopicName() {
		return topicName;
	}
	public void setTopicName(String topicName) {
		this.topicName = topicName;
	}
	
}
